<?php
require_once 'base.php';

$tpl = new Template('t');
$tpl->setFile ( "main", "main.html" );
$tpl->setFile ( "index", "index.html" );
$tpl->setBlock ( "index", "search_title_block", "search_title_blocks" );
$tpl->setBlock ( "index", "article", "articles" );

$db = conn();

// ------------- 页面基本信息 -------------
// 分类
$category_str = '';
foreach ($CFG['category'] as $key => $category){
	$category_str .= "<li class=\"cat-item\"><a href=\"index.php?search=category&category={$key}\" title=\"查看所有分类为 {$category} 的文章\">{$category}</a></li>";
}
// 标签
$sql_tags = "SELECT * FROM tags ORDER BY amount DESC LIMIT 25";
$rs=$db->query($sql_tags, MYSQL_ASSOC);
$tag_str = '';
foreach($rs as $tag){
    $tag['name'] = stripslashes($tag['name']);
	$tag_str .= "<a href=\"index.php?search=tag&tag={$tag['name']}\" title=\"查看所有标记为 {$tag['name']} 的文章\">{$tag['name']}</a> ";
}
// 显示
$tpl->setVar(array(
    'website_url' => $CFG['website']['url'],
    'web_title' => $CFG['website']['page_title'],
	'description' => $CFG['website']['description'],
	'category_str' => $category_str,
	'tag_str' => $tag_str
));
// ------------- end 页面基本信息 -------------

// ------------- 显示list -------------
$where = " WHERE status>-1";
// 组织查询条件
$search = fget('search', 0);
if ($search){
	$search_title = '';
	switch ($search){
		case 'search':
			$search_word = addslashes(fget('search_word', NULL));
			if (strlen_utf8($search_word) < 21 && $search_word != "输入关键词"){
				if (!empty($search_word)){
					$where .= " AND (contents LIKE '%{$search_word}%' OR title LIKE '%{$search_word}%')";
					$search_title = "搜索“{$search_word}”的文章";
				}
			}
		break;
		case 'category':
			$category = getNum(fget('category', 0));
			if ($category){
				$where .= " AND category={$category}";
				$search_title = "属于分类“{$CFG['category'][$category]}”的文章";
			}
		break;
		case 'tag':
			$tag = addslashes(fget('tag', NULL));
			if (!empty($tag)){
				$where .= " AND tags LIKE '%{$tag}%'";
				$search_title = "被标记为“{$tag}”的文章";
			}
	}

	$tpl->setVar("search_title", $search_title);
	$tpl->parse("search_title_blocks", "search_title_block", true);

    $tpl->setVar('page_title', $search_title . " - " . $CFG['website']['page_title']); // 页面title
}else{
    $tpl->setVar('page_title', $CFG['website']['page_title']); // 页面title
}

$sql = "SELECT COUNT(*) FROM article" . $where;
$rs=$db->query($sql, MYSQL_NUM);

if($rs[0][0] > 0){
	$page_str = pageft($rs[0][0],20);

	$sql = "SELECT * FROM article" . $where . " ORDER BY id DESC {$page_str['limit']}";
	$rs=$db->query($sql, MYSQL_ASSOC);
	foreach($rs as $v){
		// 标签文字加链接
		if (!empty($v['tags'])){
            $v['tags'] = stripslashes($v['tags']);
			$tag_arr = explode(",", $v['tags']);
			$tags_str = "";
			foreach ($tag_arr as $tag){
				$tags_str .= "<a href=\"index.php?search=tag&tag={$tag}\">{$tag}</a> , ";
			}
			$tags_str = substr($tags_str, 0, -3);
		}else{
			$tags_str = "没有标签";
		}
		// 分类文字加链接
		$category_str = $v['category'] ? "<a href=\"index.php?search=category&category={$v['category']}\">{$CFG['category'][$v['category']]}</a>" : "没有分类";

		$tpl->setVar(array(
			"aid" => $v['aid'],
			"title" => stripslashes($v['title']),
            "contents" => nl2br(strip_tags(stripslashes($v['headline']))),
			"category" => $category_str,
			"tags" => stripslashes($tags_str),
			"month" => date("n", $v['submit_time']),
			"date" => date("d", $v['submit_time']),
			"submit_time" => date("h:i A", $v['submit_time']),
			"amount" => $v['amount'],
			"comment" => $v['comment'] //评论数
		));

		$tpl->parse("articles", "article", true);
	}
	$tpl->setVar("page_str", $page_str["str"]);
}else{
	$tpl->setVar("no_article", "<div class=\"no_article\">哎呀，这是跑哪了，怎么一个人也没有啊，还是<a href=\"javascript:void(0);\" onclick=\"javascript:history.back();\">回去</a>吧！^_^</div>");
}

$db->close();

$tpl->parse('main_content', 'index');
$tpl->p("main");
?>